package com.sprucetec.live.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sprucetec.live.common.dto.business.LiveChatDto;
import com.sprucetec.live.common.dto.business.LiveVideoDto;
import com.sprucetec.live.common.webcontext.LoginUserContext;
import com.sprucetec.live.entity.LiveAnchor;
import com.sprucetec.live.entity.LiveCategory;
import com.sprucetec.live.common.dto.PageDTO;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.business.LiveCategoryDto;
import com.sprucetec.live.entity.LiveVideo;
import com.sprucetec.live.enums.DeleteStatusEnum;
import com.sprucetec.live.mapper.LiveCategoryMapper;
import com.sprucetec.live.service.LiveCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @ClassName LiveCategoryServiceImpl
 * @Description:
 * @Author Kenny
 * @Date 2020/3/30
 **/
@Service("liveCategoryService")
@Slf4j
public class LiveCategoryServiceImpl implements LiveCategoryService {

    @Autowired
    private LiveCategoryMapper liveCategoryMapper;

    /**
     * 获取所有直播分类数据
     */
    @Override
    public PagedListDTO<LiveCategoryDto> getCategoryPage(LiveCategoryDto liveCategoryDto) {
        PagedListDTO<LiveCategoryDto> pagedListDTO = new PagedListDTO<>();
        PageHelper.startPage(liveCategoryDto.getPageIndex(), liveCategoryDto.getPageSize());

        Example example = new Example(LiveCategory.class);
        example.createCriteria().andEqualTo("isDeleted",DeleteStatusEnum.VALID.getCode());
        if(StringUtils.isNotBlank(liveCategoryDto.getCategoryName())){
            example.createCriteria().andLike("categoryName", String.valueOf(liveCategoryDto.getCategoryName()));
        }
        List<LiveCategory> liveCategories = liveCategoryMapper.selectByExample(example);
        PageInfo<LiveCategory> pages = new PageInfo(liveCategories);
        List<LiveCategoryDto> liveCategoryDtos = pages.getList().stream().map(x -> {
            LiveCategoryDto chatDto = new LiveCategoryDto();
            BeanUtils.copyProperties(x, chatDto);
            return chatDto;
        }).collect(Collectors.toList());

        pagedListDTO.setTotal((int) pages.getTotal());
        pagedListDTO.setRecords(liveCategoryDtos);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(liveCategoryDto.getPageSize());
        pageDTO.setPageIndex(liveCategoryDto.getPageIndex());
        pagedListDTO.setPage(pageDTO);
        return pagedListDTO;
    }

    @Override
    public int createCategory(LiveCategory liveCategory) {
        liveCategory.setCreateTime(new Date());
        liveCategory.setUpdateTime(new Date());
        liveCategory.setCreateUserCode(LoginUserContext.getUser().getUid());
        liveCategory.setCreateUserName(LoginUserContext.getUser().getName());
        liveCategory.setUpdateUserCode(LoginUserContext.getUser().getUid());
        liveCategory.setUpdateUserName(LoginUserContext.getUser().getName());
        return liveCategoryMapper.insertSelective(liveCategory);
    }

    @Override
    public int updateCategory(LiveCategory liveCategory) {

        liveCategory.setUpdateTime(new Date());
        liveCategory.setUpdateUserCode(LoginUserContext.getUser().getUid());
        liveCategory.setUpdateUserName(LoginUserContext.getUser().getName());
        return liveCategoryMapper.updateByPrimaryKeySelective(liveCategory);
    }

    @Override
    public Map<Long, LiveCategory> getAllCategoryMap() {
        List<LiveCategory> allCategoryList = getCategoryList();
        Map<Long, LiveCategory> categoryMap = new HashMap<>(allCategoryList.size());
        for (LiveCategory category : allCategoryList) {
            categoryMap.put(category.getId(), category);
        }
        return categoryMap;
    }

    /**
     * 校验分类是否重复
     * @param liveCategory
     * @return
     */
    @Override
    public String checkCategory(LiveCategory liveCategory) {
        if (StringUtils.isNotBlank(liveCategory.getCategoryName())) {
            LiveCategory queryBean = new LiveCategory();
            queryBean.setId(liveCategory.getId() == null ? null : liveCategory.getId());
            queryBean.setCategoryName(liveCategory.getCategoryName());
            int num = liveCategoryMapper.countLiveCategoryList(queryBean);
            if (num > 0) {
                return "分类名称已存在";
            }
            queryBean.setCategoryName(null);
        }
        return null;
    }

    public List<LiveCategory> getCategoryList() {
        return liveCategoryMapper.selectAll();
    }

}
